// pages/EmpLaber/EmpLaber.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        category: [],//作品类型
        categoryid: -1,//作品类型选中id
        categorychild: [],//作品类型显示子集
        childid: 0,//子类型选中id
        childshow: false,//子类型是否显示
        ty: 1,//操作类型,1新增,2修改
        opus: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var tp = parseInt(options.tp);
        if (tp == 2) {
            var opus = JSON.parse(options.opus);
            _this.setData({
                opus: opus
            })
        }
        _this.setData({
            tp: tp
        })
        _this.GetCagetory();
    },
    //获取作品类型
    GetCagetory: function () {
        var _this = this;
        app.http.POST('/MiniManagerEmp/GetOpusCategory', {}, (err, data) => {
            if (data.code == 1) {
                var category = data.data;
                _this.setData({
                    category: category
                });
                var opus = _this.data.opus;
                if (_this.data.tp == 2 && opus) {
                    var CategoryId = opus.CategoryId;
                    if (CategoryId == null) {
                        _this.setData({
                            categoryid: -1,
                            childid: 0,
                            childshow: false
                        });
                    } else if (CategoryId == 0) {
                        _this.setData({
                            categoryid: 0,
                            childid: 0,
                            childshow: false
                        });
                    } else {
                        category.forEach(function(item,index){
                            if (item.ID == CategoryId) {
                                _this.setData({
                                    categoryid: CategoryId,
                                    childid: 0,
                                    childshow: false
                                });
                                return;
                            }
                            if (item.Child){
                                item.Child.forEach(function(citem,cindex){
                                    if (citem.ID == CategoryId) {
                                        _this.setData({
                                            categoryid: item.ID,
                                            childid: citem.ID,
                                            childshow: true,
                                            categorychild: item.Child
                                        });
                                        return;
                                    }
                                })
                            }
                            
                        })
                    }
                }
            }
            else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
            wx.hideLoading();
        });
    },
    //作品类型改变
    CategoryChange: function (e) {
        var _this = this;
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        var oldid = _this.data.categoryid;
        var childshow = _this.data.childshow;
        if (id == 0) {
            _this.setData({
                categoryid: id,
                categorychild: [],
                childshow: false,
                childid: 0,
            });
        } else {
            if (oldid == id) {
                _this.setData({
                    childshow: !childshow,
                    childid: 0,
                });
                return;
            }
            var category = _this.data.category;
            var categorychild = [];
            if (category && category.length > 0) {
                category.forEach(function (item, index) {
                    if (item.ID == id) {
                        categorychild = item.Child;
                    }
                })
            }
            _this.setData({
                categoryid: id,
                categorychild: categorychild,
                childid: 0,
                childshow: true
            });
        }
    },
    //子作品类型改变
    ChildChange: function (e) {
        var _this = this;
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        _this.setData({
            childid: id
        })
    },
    //添加作品图片
    AddImg: function (e) {
        var _this = this;
        var count = _this.data.opus.ImgUrls ? _this.data.opus.ImgUrls.length : 0;
        if (count >= 5) {
            wx.showToast({
                title: "最多可以上传5张图片",
                icon: 'none',
                duration: 2000
            })
            return;
        }
        var setting = {
            iscut: false,
            maxcount: 5 - count,
            param: {
                category: 3
            }
        }
        wx.navigateTo({
            url: '../imgupload/imgupload?setting=' + JSON.stringify(setting)
        });
    },
    //提交数据
    formSubmit: function (e) {
        var _this = this;
        var tp = _this.data.tp;
        var param = e.detail.value;
        //选择的作品类型
        if (_this.data.childid == 0) {
            if (_this.data.categoryid == -1) {
                delete param.CategoryId;
            } else {
                param["CategoryId"] = _this.data.categoryid
            }
        } else {
            param["CategoryId"] = _this.data.childid
        }
        var ImgUrls = _this.data.opus.ImgUrls;
        if (!ImgUrls || ImgUrls.length < 1) {
            wx.showToast({
                title: "请上传图片",
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (ImgUrls) {
            param["Zpimg"] = ImgUrls.join(",");
        }
        if (tp == 2)//修改
        {
            param["ID"] = _this.data.opus.ID
        }
        wx.showLoading({
            title: '发布中',
        })
        app.http.POST('/MiniManagerEmp/SaveOpus', param, (err, data) => {
            if (data.code == 1) {
                wx.showToast({
                    title: "发布成功",
                    icon: 'none',
                    duration: 2000,
                    success: function () {
                        if(_this.data.tp!=3){
                            let pages = getCurrentPages();//当前页面
                            let prevPage = pages[pages.length - 2];//上一页面
                            prevPage.GetOpusList();
                        }
                        wx.navigateBack({

                        })
                    }
                })
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
            wx.hideLoading();
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      wx.setNavigationBarTitle({
        title: '作品发布'
      })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var _this = this;
        var ImgUrls = [];
        if (_this.data.opus.ImgUrls) {
            ImgUrls = _this.data.opus.ImgUrls;
        }
        var FilePaths = _this.data.FilePaths;
        if (FilePaths) {
            FilePaths.forEach(function (item, index) {
                ImgUrls.push(item);
            })
        }
        _this.setData({
            "opus.ImgUrls": ImgUrls,
            "FilePaths": []
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})